package com.gurumades.jsonform.server.forms.dispatch;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.node.ObjectNode;

import com.gurumades.jsonform.shared.dispatch.JsonHelper;

public class ServerSideJsonHelper extends JsonHelper{

	ObjectMapper m = new ObjectMapper();
	
	@SuppressWarnings("rawtypes")
	@Override
	public Map wrapItem(Object jsonImpl) {
		ObjectNode node = null;
		if (jsonImpl instanceof Map) {
			return (Map) jsonImpl;
		}else if (jsonImpl instanceof ObjectNode){
			node = (ObjectNode)jsonImpl;
		} else {
			return null;
		}
		
		try {
			Map item = m.readValue(node, Map.class);
			return item;
		} catch (JsonParseException e) {
		} catch (JsonMappingException e) {
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return new HashMap();
	}

}
